package com.sumainfor.pc.dev.service.impl;

import com.sumainfor.common.util.ContextUtils;
import com.sumainfor.common.util.CustomMap;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.utlis.ParamsCommon;
import com.sumainfor.pc.dev.dao.PopularHousingMapper;
import com.sumainfor.pc.dev.feign.Zfeign.ZpcMktFeign;
import com.sumainfor.pc.dev.service.PopularHousingService;
import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;

/**
 * <p>
 * 热门房源
 * <p>
 *
 * @author: zhouocng
 * @create: 2019-05-09 16:02
 **/
@Service
public class PopularHousingServiceImpl implements PopularHousingService {
    private ReentrantLock lock = new ReentrantLock(false);
    Logger log = LoggerFactory.getLogger(PopularHousingServiceImpl.class);

    @Autowired
    PopularHousingMapper popularHousingMapper;

    @Autowired
    ZpcMktFeign zpcMktFeign;

    /**
    * <p>
    * 获取热门房源(首页热门房源)
    * </p>
    *
    * @author: zhoucong
    * @since:
    */
    @Override
    public JsonResult getIfPopularList(Map<String,Object> params){
        JsonResult result = new JsonResult();
        //查询首页热门房源
        if (MapUtils.getInteger(params,"State")!= null&&MapUtils.getInteger(params,"State")==1) {
            Integer cout  =popularHousingMapper.getZongPopular();//总的数据
            System.out.println("总的数据"+cout);
            List<String> HouInfoIdList = new ArrayList<>();
            if ( cout> 4) {//总的数量>4
                //查首页热门房源数量
                Integer a = popularHousingMapper.getIfPopularCount();
                System.out.println("数量大于4中首页数量"+a);
                if (a < 4) {//首页热门房源数量小4
                    List<Map<String, Object>> zong1List = popularHousingMapper.getIfPopularList(CustomMap.create("IfPopular", 1));
                    for (Map<String, Object> zong1Map : zong1List) {
                        HouInfoIdList.add(MapUtils.getString(zong1Map, "HouInfoId"));
                    }
                    Map<String, Object> ipMap = new HashMap<>();
                    Integer ceshi =0;
                    ipMap.put("IfPopular",ceshi);
                    ipMap.put("limit",4 - a);
                    System.out.println("查询非首页房源的数量"+ipMap);
                    List<Map<String,Object>> OneList = popularHousingMapper.getIfPopularList(ipMap);

                   // List<Map<String, Object>> list = popularHousingMapper.getIfPopularList(CustomMap.create("IfPopular", 0).put("limit",4 - a));
                    System.out.println("测试的非首页热门房源的信息"+OneList);
                    for (Map<String, Object> listMap : OneList) {
                        HouInfoIdList.add(MapUtils.getString(listMap, "HouInfoId"));
                    }

                    if (HouInfoIdList.size()>0){
                        params.put("HouInfoIdList",HouInfoIdList);
                        result =zpcMktFeign.selectHousingHouInfoId(params);

                    }
                }if (a >= 4) {//首页热门房源大于4
                    Map<String, Object> ipMap = new HashMap<>();
                    ipMap.put("IfPopular", 1);
                    ipMap.put("limit", 4);
                    List<Map<String, Object>> list1 = popularHousingMapper.getIfPopularList(ipMap);
                    for (Map<String, Object> list1Map : list1) {
                        HouInfoIdList.add(MapUtils.getString(list1Map, "HouInfoId"));
                    }

                    if (HouInfoIdList.size()>0){
                        params.put("HouInfoIdList",HouInfoIdList);
                        result =zpcMktFeign.selectHousingHouInfoId(params);
                    }
                }
                //不能展示的房源信息
                List<Map<String,Object>> buzhanshiList = popularHousingMapper.getIfPopularList(params);
                Map<String,Object> buzhanshimap = new HashMap<>();
                buzhanshimap.put("HouInfoIdList",buzhanshiList);
              //  result.put(buzhanshimap);
               List<Map<String,Object>> resultList = (List<Map<String, Object>>) result.get("result");
                resultList.add(buzhanshimap);


            }if (cout<=4){//总的数量
                Map<String,Object> xeshiMap = new HashMap<>();
               List<Map<String,Object>>  buzuList =  popularHousingMapper.getIfPopularList(xeshiMap);
               if (buzuList.size()>0) {
                   for (Map<String, Object> buzuMap : buzuList) {
                       HouInfoIdList.add(MapUtils.getString(buzuMap,"HouInfoId"));
                   }
                   if (HouInfoIdList.size()>0){
                       params.put("HouInfoIdList",HouInfoIdList);
                       result =zpcMktFeign.selectHousingHouInfoId(params);
                   }
                   List<String> houList = new ArrayList<>();
                   xeshiMap.put("HouInfoIdList",houList);
                   //result.put(xeshiMap);
                   List<Map<String,Object>> resultList = (List<Map<String, Object>>) result.get("result");
                   resultList.add(xeshiMap);

               }else{
                   List<String> houList = new ArrayList<>();
                   xeshiMap.put("HouInfoIdList",houList);
                   result.put(xeshiMap);
               }



            }


        }else{//查询热门房源
            List<String> HouInfoIdList = new ArrayList<>();
            Map<String, Object> ipMap = new HashMap<>();
            ipMap.put("IfPopular", 0);
            List<Map<String,Object>> zczsList= popularHousingMapper.getIfPopularList(ipMap);
            if (zczsList.size()>0) {
                for (Map<String, Object> zczsMap : zczsList) {
                HouInfoIdList.add(MapUtils.getString(zczsMap,"HouInfoId"));
                }
                if (HouInfoIdList.size()>0){
                    params.put("HouInfoIdList",HouInfoIdList);
                    result =zpcMktFeign.selectHousingHouInfoId(params);
                    //房源池中不能展示的数据
                    Map<String,Object> noHouMap = new HashMap<>();
                    noHouMap.put("HouInfoIdList",HouInfoIdList);

                    List<Map<String,Object>> noHouList = popularHousingMapper.getIfPopularList(noHouMap);
                    if (noHouList.size()>0&&noHouList !=null) {
                        noHouMap.put("HouInfoIdList", noHouList);
                        System.out.println("不能展示的房源数据"+HouInfoIdList);
                      //  result.put(noHouMap);
                        List<Map<String,Object>> resultList = (List<Map<String, Object>>) result.get("result");
                        resultList.add(noHouMap);
                    }else{
                        List<String> houList = new ArrayList<>();
                        noHouMap.put("HouInfoIdList",houList);
                     //   result.put(noHouMap);
                        List<Map<String,Object>> resultList = (List<Map<String, Object>>) result.get("result");
                        resultList.add(noHouMap);
                    }

                }

            }else{
                Map<String,Object> noHouMap = new HashMap<>();
                noHouMap.put("IfPopular",1);
                List<Map<String,Object>> noHouList = popularHousingMapper.getIfPopularList(noHouMap);
                System.out.println("noHouList"+noHouList);
                if (noHouList.size()>0&&noHouList !=null) {
                    noHouMap.put("HouInfoIdList", noHouList);
                    System.out.println("不能展示的房源数据"+noHouMap);
                    result.put(noHouMap);
                }else{
                    List<String> houList = new ArrayList<>();
                    noHouMap.put("HouInfoIdList",houList);
                    result.put(noHouMap);
                }

            }


        }
    return  result;

        }

    /**
    * <p>
    * 保存热门房源
    * </p>
    *
    * @author: zhoucong
    * @since:
    */
    @Override
    public JsonResult savePopular(Map<String,Object> params){
        JsonResult result = new JsonResult();
        /**
         * 加锁操作
         */
        lock.lock();
        try {
            log.info("更新操作");
            System.out.println("params"+params);
           //A查询的房源详表
            List<Map<String,Object>> HouInfoIdOneList = (List<Map<String, Object>>) params.get("HouInfoIdOneList");
            System.out.println("查询的房源详表"+HouInfoIdOneList);
          //B传过来的房源详表编号集合
            List<Map<String,Object>> HouInfoIdList = (List<Map<String, Object>>) params.get("HouInfoIdList");
            System.out.println("传过来的房源详表编号集合"+HouInfoIdList);
            if ( params.get("HouInfoIdOneList")!=null||params.get("HouInfoIdList")!=null) {
                if (HouInfoIdOneList.size() > 0 || HouInfoIdList.size() > 0) {
                       for (Map<String,Object> HouInfoIdOne:HouInfoIdOneList) {
                          System.out.println("需要删除的房源"+HouInfoIdOne);
                           Integer count1 = popularHousingMapper.deletePopular(HouInfoIdOne);
                        if(count1<=0){
                                result.putFailed("保存失败!");
                          }else{
                              result.putSuccess("保存成功!");
                         }
                       }
                       for (Map<String,Object> HouInfoId:HouInfoIdList) {
                            HouInfoId.put("IfPopular", params.get("State"));
                           HouInfoId.put("by", ContextUtils.getUserId());//操作人
                          HouInfoId.put("term","7");//1 租客端 2市场部 3 工程部 4运营部 5财务部 6总经办 7发展部
                          HouInfoId.put("fnc",
                                  this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());// 创建Function名
                           ParamsCommon.commonInsertParms(HouInfoId);
                           System.out.println("需要增加的房源"+HouInfoId);
                            Integer count2 = popularHousingMapper.insertPopular(HouInfoId);
                          if(count2<=0){
                              result.putFailed("保存失败!");
                          }else{
                              result.putSuccess("保存成功!");
                           }
                       }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//事务回滚
            result.putFailed("服务器繁忙");
        } finally {
            lock.unlock();
        }
        return result;
    }
}
